using ASP;
using HoWave.Alipay;
using HoWave.Common;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
public class paygift_return_url : Member, IRequiresSessionState
{
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		SortedDictionary<string, string> requestGet = this.GetRequestGet();
		if (requestGet.Count > 0)
		{
			Notify notify = new Notify();
			bool flag = notify.Verify(requestGet, base.Request.QueryString["notify_id"], base.Request.QueryString["sign"]);
			if (flag)
			{
				string text = base.Request.QueryString["trade_no"];
				string text2 = base.Request.QueryString["out_trade_no"];
				string text3 = base.Request.QueryString["total_fee"];
				string text4 = base.Request.QueryString["subject"];
				string text5 = base.Request.QueryString["body"];
				string text6 = base.Request.QueryString["buyer_email"];
				string text7 = base.Request.QueryString["trade_status"];
				string text8 = base.Request.QueryString["extra_common_param"];
				if (base.Request.QueryString["trade_status"] == "TRADE_FINISHED" || base.Request.QueryString["trade_status"] == "TRADE_SUCCESS")
				{
					PayOnline payOnline = new PayOnline();
					payOnline.SetPayOK(text2);
					string[] array = text8.Split(new char[]
					{
						'_'
					});
					if (array.Length < 3)
					{
						this.WriteErrMsg("", Language.GetMemberMsg("Voucher/DataIllegal", new string[0]), "");
					}
					int num = 0;
					int receiveid = 0;
					string text9 = array[0];
					if (array[1] != "")
					{
						num = Convert.ToInt32(array[1]);
					}
					if (array[2] != "")
					{
						receiveid = Convert.ToInt32(array[2]);
					}
					int num2 = (int)this.conn.GetScalar("Select UserID From HL_PayOnline WHERE PayNum= '" + text2 + "'", new IDbDataParameter[0]);
					if (num == 0)
					{
						num = num2;
					}
					if (!this.UpdateUserGiftBuyedCard(text9, num, receiveid))
					{
						this.WriteErrMsg("", Language.GetMemberMsg("Voucher/AddGiftError", new string[0]), "");
					}
					UserGiftCardModel model = Blls.bllGiftCard.GetModel(text9);
					if (model != null && model.RecieveID > 0)
					{
						string userNameByid = StaticFun.GetUserNameByid(model.RecieveID);
						string userNameByid2 = StaticFun.GetUserNameByid(model.BuyID);
						string memberMsg = Language.GetMemberMsg("Voucher/SendGiftEmailContent", new string[]
						{
							userNameByid,
							userNameByid2,
							model.BuyTime.ToString(),
							this.config["SiteUrl"].ToString()
						});
						MessageBll messageBll = new MessageBll(this.conn);
						string title = "礼品卡接收提醒";
						messageBll.AddMessage(userNameByid2, "Users", title, memberMsg, 0, 0, 1, 0, userNameByid, "", 1);
						Email email = new Email();
						email.SendEmail(this.config, memberMsg, model.ReceiveEmail, Language.GetMemberMsg("Voucher/SendGiftEmailTitle", new string[0]));
					}
					base.Response.Redirect("~/Member/MBF_MyGiftCards.aspx");
				}
				else
				{
					base.Response.Write("trade_status=" + base.Request.QueryString["trade_status"] + "支付操作未完成！");
				}
			}
			else
			{
				base.Response.Write("验证失败！请与网站管理员联系。");
			}
		}
		else
		{
			base.Response.Write("无返回参数");
		}
	}
	public SortedDictionary<string, string> GetRequestGet()
	{
		SortedDictionary<string, string> sortedDictionary = new SortedDictionary<string, string>();
		NameValueCollection queryString = base.Request.QueryString;
		string[] allKeys = queryString.AllKeys;
		for (int i = 0; i < allKeys.Length; i++)
		{
			sortedDictionary.Add(allKeys[i], base.Request.QueryString[allKeys[i]]);
		}
		return sortedDictionary;
	}
	private bool UpdateUserGiftBuyedCard(string cardnumber, int buyid, int receiveid)
	{
		bool flag = false;
		bool result;
		if (cardnumber == "" || buyid < 1)
		{
			result = false;
		}
		else
		{
			UserGiftCardModel model = Blls.bllGiftCard.GetModel(cardnumber);
			if (model != null)
			{
				this.conn.Execute(string.Concat(new object[]
				{
					"UPDATE [HL_User] SET [Balance] = [Balance]+",
					model.CouponAmount,
					" WHERE [UserID] = ",
					base.user.UserID
				}));
				model.BuyID = new int?(buyid);
				model.RecieveID = new int?(receiveid);
				Blls.bllGiftCard.Update(model);
				UserCardRecordModel userCardRecordModel = new UserCardRecordModel();
				userCardRecordModel.Amount = model.FaceValue;
				userCardRecordModel.Remark = "购买礼品卡";
				userCardRecordModel.CardType = "2";
				userCardRecordModel.UserID = new int?(base.user.UserID);
				userCardRecordModel.CardNumber = cardnumber;
				if (userCardRecordModel.CardNumber != "")
				{
					Blls.bllUserCardRecord.Add(userCardRecordModel);
				}
				if (model.RecieveID > 0)
				{
					userCardRecordModel.UserID = model.RecieveID;
					userCardRecordModel.Remark = "用户赠送您礼品卡";
					Blls.bllUserCardRecord.Add(userCardRecordModel);
					Email email = new Email();
					string memberMsg = Language.GetMemberMsg("Voucher/SendGiftEmailContent", new string[]
					{
						StaticFun.GetUserNameByid(model.RecieveID),
						StaticFun.GetUserNameByid(model.BuyID),
						model.BuyTime.ToString(),
						this.config["SiteUrl"].ToString()
					});
					email.SendEmail(this.config, memberMsg, model.ReceiveEmail, Language.GetMemberMsg("Voucher/SendGiftEmailTitle", new string[0]));
				}
				flag = true;
			}
			result = flag;
		}
		return result;
	}
}
